import 'package:flutter/material.dart';
import '../../model/Bus.dart';
import '../../res.dart';
import '../../utils/color/ColorUtils.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../config/Constant.dart';
import '../../model/Sort.dart';
import '../../utils/EventBus/EventBus.dart';

class SortDialog extends StatefulWidget {
  const SortDialog({Key? key}) : super(key: key);

  @override
  State<SortDialog> createState() => _SortDialogState();
}

class _SortDialogState extends State<SortDialog> {
  List<Sort> list = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    list.add(Sort("last".tr, "new_to_old".tr, Res.sys_icon_seq_otn));
    list.add(Sort("last".tr, "old_to_new".tr, Res.sys_icon_seq_nto));
    list.add(Sort("name".tr, "A_Z".tr, Res.sys_icon_seq_naz));
    list.add(Sort("name".tr, "Z_A".tr, Res.sys_icon_seq_nza));
    setSelect(SpUtil.getInt(Constant.sort)!);
    setState(() {});
  }

  void setSelect(int index) {
    SpUtil.putInt(Constant.sort, index);
    for (int i = 0; i < list.length; i++) {
      if (i == index) {
        list[i].select = true;
      } else {
        list[i].select = false;
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(12)),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            margin: const EdgeInsets.only(bottom: 10),
            child: Row(
              children: [
                TextView("sort_by".tr,
                    style: TextStyle(
                        color: ColorUtils.text,
                        fontSize: 18,
                        fontWeight: FontWeight.bold)),
                const Spacer(),
                MyLocalImage(
                  Res.sys_icon_delete_grey3x,
                  width: 20,
                  height: 20,
                  onclick: () {
                    Get.back();
                  },
                ),
              ],
            ),
          ),
          Expanded(
              child: ListView.builder(
            itemBuilder: (c, index) {
              return InkWell(
                onTap: () {
                  setSelect(index);
                },
                child: Column(
                  children: [
                    const Divider(),
                    Container(
                      height: 62,
                      alignment: Alignment.center,
                      child: Row(
                        children: [
                          MyLocalImage(
                            list[index].icon,
                            width: 20,
                            height: 20,
                            margin: const EdgeInsets.only(right: 20),
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              TextView(
                                list[index].name,
                                alignment: Alignment.centerLeft,
                                style: TextStyle(
                                    color: ColorUtils.text, fontSize: 16),
                              ),
                              TextView(list[index].tips,
                                  alignment: Alignment.centerLeft,
                                  margin: const EdgeInsets.only(top: 2),
                                  style: TextStyle(
                                      color: ColorUtils.text_, fontSize: 12)),
                            ],
                          ),
                          const Spacer(),
                          if (list[index].select)
                            const MyLocalImage(
                              Res.list_icon_select_check_red,
                              width: 20,
                              height: 20,
                            ),
                        ],
                      ),
                    ),
                  ],
                ),
              );
            },
            itemCount: list.length,
          )),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextView(
                "cancel".tr,
                width: 140,
                height: 45,
                alignment: Alignment.center,
                backcolor: rgba(247, 247, 247, 1),
                radius: BorderRadius.circular(6),
                style: TextStyle(
                    color: ColorUtils.text_, fontWeight: FontWeight.bold),
              ),
              const SizedBox(
                width: 16,
              ),
              TextView(
                "ok".tr,
                width: 140,
                height: 45,
                alignment: Alignment.center,
                radius: BorderRadius.circular(6),
                backcolor: rgba(255, 78, 77, 1),
                style: const TextStyle(
                    color: Colors.white, fontWeight: FontWeight.bold),
                onclick: () {
                  Get.back();
                  EventBusUtil.fireEvent(
                      Bus(Constant.SORT_MSG, SpUtil.getInt(Constant.sort)));
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}
